/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package modulos.respaldosBD;

import javax.swing.JOptionPane;
import vista.modelo.OperacionesVarias;

/**
 *
 * @author francisco
 */
public class FrmGeneracion extends javax.swing.JInternalFrame implements Runnable {

    String dir;
    String user;
    String clave;
    int tipo;
    Thread hilo;

    /**
     * Creates new form FrmGeneracion
     */
    public FrmGeneracion() {
        initComponents();
    }

    public FrmGeneracion(String dir, String user, String clave, int tipo) {
        initComponents();
        OperacionesVarias.centrar(this);
        this.dir = dir;
        this.user = user;
        this.clave = clave;
        this.tipo = tipo;
        hilo = new Thread(this);
        hilo.start();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jProgressBar1 = new javax.swing.JProgressBar();
        jLabel1 = new javax.swing.JLabel();
        lblInfo = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();

        setTitle("RESPALDO DE LA INFORMACION");
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/harddrive2.png"))); // NOI18N

        jProgressBar1.setIndeterminate(true);

        lblInfo.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/accept.png"))); // NOI18N
        jButton1.setText("Aceptar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(28, 28, 28)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 316, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 199, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(59, 59, 59)
                        .addComponent(lblInfo, javax.swing.GroupLayout.PREFERRED_SIZE, 256, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(136, 136, 136)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(50, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 11, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(lblInfo, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addContainerGap(16, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        dispose();
    }//GEN-LAST:event_jButton1ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JProgressBar jProgressBar1;
    private javax.swing.JLabel lblInfo;
    // End of variables declaration//GEN-END:variables

    @Override
    public void run() {
        if (tipo == 1) {
            jLabel1.setText("Respaldando Base de Datos...");
            if (BackupBD.generarBackup(dir, user, clave)) {
                dispose();
                JOptionPane.showMessageDialog(null, "Respaldo generado correctamente", "RESPALDO", javax.swing.JOptionPane.INFORMATION_MESSAGE);
            }
        }
        else if (tipo == 2) {
            jLabel1.setText("Cargando Backup Base de Datos...");
            if (BackupBD.restaurarBackup(dir, user, clave)) {
                dispose();
                JOptionPane.showMessageDialog(null, "Backup cargado correctamente", "RESPALDO", javax.swing.JOptionPane.INFORMATION_MESSAGE);
            }
        }

    }
}
